package com.example.demo.service.impl;

import com.example.demo.entity.Like;
import com.example.demo.mapper.LikeMapper;
import com.example.demo.mapper.PostMapper;
import com.example.demo.mapper.CommentMapper;
import com.example.demo.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    private LikeMapper likeMapper;

    @Autowired
    private PostMapper postMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Override
    @Transactional
    public void likePost(Long userId, Long postId) {
        // 检查是否已经点赞
        if (likeMapper.selectLikeByUserAndPost(userId, postId) != null) {
            throw new RuntimeException("You have already liked this post");
        }

        // 创建点赞记录
        Like like = new Like(userId, postId, null);
        likeMapper.insertLike(like);

        // 更新帖子点赞数
        postMapper.incrementLikeCount(postId);
    }

    @Override
    @Transactional
    public void unlikePost(Long userId, Long postId) {
        // 检查是否有点赞记录
        if (likeMapper.selectLikeByUserAndPost(userId, postId) == null) {
            throw new RuntimeException("You haven't liked this post yet");
        }

        // 删除点赞记录
        likeMapper.deleteLikeByUserAndPost(userId, postId);

        // 更新帖子点赞数
        postMapper.decrementLikeCount(postId);
    }

    @Override
    @Transactional
    public void likeComment(Long userId, Long commentId) {
        // 检查是否已经点赞
        if (likeMapper.selectLikeByUserAndComment(userId, commentId) != null) {
            throw new RuntimeException("You have already liked this comment");
        }

        // 创建点赞记录
        Like like = new Like(userId, null, commentId);
        likeMapper.insertLike(like);

        // 更新评论点赞数
        commentMapper.incrementLikeCount(commentId);
    }

    @Override
    @Transactional
    public void unlikeComment(Long userId, Long commentId) {
        // 检查是否有点赞记录
        if (likeMapper.selectLikeByUserAndComment(userId, commentId) == null) {
            throw new RuntimeException("You haven't liked this comment yet");
        }

        // 删除点赞记录
        likeMapper.deleteLikeByUserAndComment(userId, commentId);

        // 更新评论点赞数
        commentMapper.decrementLikeCount(commentId);
    }

    @Override
    public boolean isPostLikedByUser(Long userId, Long postId) {
        return likeMapper.selectLikeByUserAndPost(userId, postId) != null;
    }

    @Override
    public boolean isCommentLikedByUser(Long userId, Long commentId) {
        return likeMapper.selectLikeByUserAndComment(userId, commentId) != null;
    }

    @Override
    public int getPostLikeCount(Long postId) {
        return likeMapper.countLikesByPostId(postId);
    }

    @Override
    public int getCommentLikeCount(Long commentId) {
        return likeMapper.countLikesByCommentId(commentId);
    }
}